/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'validation'
], function ($, common, ready, Collection, template, Model, dialog, Validation) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'self_media/index',
                model: {
                    url: {
                        put: _global.url.api + 'self_media/info/{id}'
                    },
                    event: {
                        '.item-edit': 'edit',
                    },
                    edit: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            title: '修改',
                            content: template('add-tpl', this.data),
                            width: 600,
                            ok: function () {
                                if (!model.put()) {
                                    return false;
                                }
                            }, cancel: function () {
                            }
                        }).show();
                        $('input,select,textarea').on('change', function (e) {
                            model.set_data($(e.target).attr('name'), $(e.target).val());
                        });
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                $('[rel=all]').addClass('active').siblings().removeClass('active');
                _this.collection.condition.status = 'all';
                _this.collection.reload();
            });

            /**
             * 按状态查询
             */
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val('').val();
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
            //保存
            $('#btn-add').on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title: '增加',
                    content: template('add-tpl', {}),
                    width: 600,
                    ok: function () {
                        var model = new Model({
                            url: _global.url.api + 'self_media/info',
                            data: _this.data
                        });
                        if (model.post()) {
                            _this.collection.reload();
                        } else {
                            return false;
                        }
                    }, cancel: function () {
                    }
                });
                _this.dialog.show();

                $('input,select,textarea').on('change', function (e) {
                    _this.change(e);
                });
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }
    };

    new index();

});